<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//header('Location:../../../../../index2.php?plugin=pixbox');

function filechooser($s=''){ return'
<form name="myform" action="plugin/pixbox/cms/filbox.php" method="post">
<div id="filebox">
<input type="button" value="x" id="bx" name="bx" onclick="testDEL();" />
<input type="file" id="pic" name="pixfile" size="127" onchange="SUB();" />
</div>
<iframe src="" id="filei" width="127" height="127">
</iframe>
</form>
<script type="text/javascript">
function submitform(){ document.myform.submit(); }
</script>
'; }


function scalepic($pic,$percent){
  $percent*=0.01;
  list($width,$height)=getimagesize($pic);
  $new_width=$width*$percent;
  $new_height=$height*$percent;
  $image_p=imagecreatetruecolor($new_width,$new_height);
  $image=imagecreatefromjpeg($pic);
  imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
  return$image_p;
}

function addpic($pid='image'){
  $file=basename($_FILES[$pid]['name']);
  $tmp=$_FILES[$pid]['tmp_name'];
  //$pixbox=$_POST[$pixbox];
  $target_path='../media/pixbox/'.$pid.'/';
  $target_file=$target_path.$file;
  if(!is_dir($target_path)){ mkdir($target_path.$pixbox); }
  if(move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
    //echo "The file ". $file . " has been uploaded";
	$image_p=scalepic($target_file);
   	$thumbs=$target_path.'thumbs/';
	if(!is_dir($thumbs)){ mkdir($thumbs); }
	$thumbs_file=$thumbs.$file;
	imagejpeg($image_p,$thumbs_file, 95);


	echo pimg($target_file);
	echo pimg ($thumbs_file);

	//header("Location:pixbox.php");
	//echo('<meta http-equiv="refresh" content="20">');
	//header('Content-type: image/jpeg');
	//return array($target_file,$thumbs_file)
} else{
	echo "There was an error uploading the file, please try again in 12 seconds or press the BACK or REFRESH button of your browser!";
	echo('<meta http-equiv="refresh" content="12">');
	//return array(False,False);
}

}
/*$testdb=pdbopen('localhost:3307','cultuuradm','test','cultuurdb');
$fields=pdbqueryfields();
pdbclose($testdb);*/

function addpic($pid='image'){
  $file=basename($_FILES[$pid]['name']);
  $tmp=$_FILES[$pid]['tmp_name'];
  //$pixbox=$_POST[$pixbox];
  $target_path='../media/pixbox/'.$pid.'/';
  $target_file=$target_path.$file;
  if(!is_dir($target_path)){ mkdir($target_path.$pixbox); }
  if(move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
    //echo "The file ". $file . " has been uploaded";
	$image_p=scalepic($target_file);
   	$thumbs=$target_path.'thumbs/';
	if(!is_dir($thumbs)){ mkdir($thumbs); }
	$thumbs_file=$thumbs.$file;
	imagejpeg($image_p,$thumbs_file, 95);


	echo pimg($target_file);
	echo pimg ($thumbs_file);

	//header("Location:pixbox.php");
	//echo('<meta http-equiv="refresh" content="20">');
	//header('Content-type: image/jpeg');
	//return array($target_file,$thumbs_file)
  }
  else{
    echo "There was an error uploading the file, please try again in 12 seconds or press the BACK or REFRESH button of your browser!";
    echo('<meta http-equiv="refresh" content="12">');
    //return array(False,False);
  }
}

function enzo(){
    echo'test';
    $pixbox=$_GET['pixbox'];
    $file=basename($_FILES[$pid]['name']);
    header("Location:../../../index2.php?site=&plugin=pixbox&page=$pixbox");

    echo getcwd();
    echo'
    <div id="fix">gvs</div>
    <script type="text/javascript">//<![CDATA[
    alert("test");
    var script=document.createElement("script");
    script.type="text/javascript";
    script.src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js";
    document.getElementById("fix").append(script);

    $(document).ready(function() {
       alert("hi");
    });
    //]]></script>
    ';
    if($handle=opendir("../images/$pixbox")){
      while(false!==($file=readdir($handle))){
        if($file=="." or $file==".."){ echo ''; }
            else{ echo "<td><img src=\"plugin/pixbox/images/$pixbox/$file\" /></td>"; }
      }
      closedir($handle);
    }
}


?>

<script type="text/javascript">//<![CDATA[
function DEL(pid,id){ document.getElementById(pid).removeChild(document.getElementById(id)); }
function changebox(){
  loadbox(document.pixform.pixlist.value);
}

function uploadpic(){
    if(document.pixform.pixbox.value.length==0){
        alert('Please type a name for your pixbox!');
    }else{ document.pixform.submit(); }
}

function loadbox(box){
  if (window.XMLHttpRequest){ var xmlhttp=new XMLHttpRequest();
  }else{ var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        eval(xmlhttp.responseText);
      //document.write(xmlhttp.responseText);
      //document.getElementById("pixdiv").innerHTML=xmlhttp.responseText;
    }
  }
  var ajax="plugin/pixbox/cms/pixbox.php?box="+box;
  xmlhttp.open("GET",ajax,true);
  xmlhttp.send();
}
//]]></script>